<template>
  <div v-html="homeWorkInfo.HomeworkDescribe">

  </div>
  <div class="w-full grid justify-items-center">
    <el-upload
        ref="upload"
        class="upload-demo mt-28"
        drag
        :headers="fileUpHead"
        action="/api/HomeWorkUpLoadFile"
        :on-success="uploadFinish"
        :data="{
        HomeworkId:homeWorkInfo.ID
      }"
        multiple
    >
      <div class="el-upload__text">
        将作业拖进框中或 <em>点击以选择作业</em>
      </div>

    </el-upload>

  </div>
  <div class="w-full grid justify-items-center mt-10">
      <p>已上传列表</p>
    <div class="mt-5" v-for="(item,index) in homeWorkFileList">
      <a class="mr-14" :download="getNameFromUrl(item.FileUrl)"
         :href="'/api/GetHomeWorkFileByUrl/'+item.FileUrl+'?tk='+fileUpHead.tk">{{ item.FileName }}</a>
      <el-button @click="WithdrawHomework(item)" type="primary">撤回</el-button>
    </div>
  </div>

</template>

<script>
import axios from "axios";

export default {
  name: "homeWorkDetail",
  data() {
    return {
      homeWorkInfo: {},
      fileUpHead: {},
      homeWorkFileList: [],
      hmwFileUploadingMap: new Map()
    }
  },
  methods: {
    uploadFinish(response, file, fileList) {
      this.$refs.upload.handleRemove(file)
      this.getFileList()
    },
    show(val) {
      this.hmwFileUploadingMap.get(val[0]).percentage = 50
      console.log(this.hmwFileUploadingMap.get(val[0]))
      this.$forceUpdate()
    },
    getNameFromUrl(item) {
      return item.split("_zjx_")[1]
    },

    getFileList: function (response, file, fileList) {
      axios.post("/api/GetHomeWorkFileListByStuIdAndHomeWorkId", {
        HomeWorkId: this.homeWorkInfo.ID,
      }).then((res) => {
        if (res.status === 200) {
          this.homeWorkFileList = res.data
        }
      })
    },
    WithdrawHomework(item) {
      axios.get("/api/WithdrawHomework", {
        params: {
          id: item.ID
        }
      }).then((response) => {
        if (response.status === 200 && response.data === 'delSuccess') {
          alert("ok")
          this.getFileList();
        }
      })
    },


  },
  created() {
    this.homeWorkInfo = this.$sestor.getSesWithParse(this.$route.query.resId)
    var parse = JSON.parse(window.localStorage.getItem('zjxClass_student_info'));
    this.fileUpHead = {
      'tk': parse.Token
    }

    this.getFileList()
  },

}
</script>

<style>
</style>